/**
 *
 */
package mblog.web.controller.desk.tags;

import java.util.List;

import mblog.base.lang.Consts;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import mblog.core.data.Tag;
import mblog.core.persist.service.PostService;
import mblog.core.persist.service.TagService;
import mblog.web.controller.BaseController;
import mblog.web.controller.desk.Views;
import mtons.modules.pojos.Paging;

import javax.servlet.http.HttpServletRequest;

/**
 * 发现
 *
 * @author langhsu
 */
@Controller
public class TagsController extends BaseController {
    @Autowired
    private PostService postService;
    @Autowired
    private TagService tagService;

    private int maxResults = 12;

    @RequestMapping("/tags")
    public String view(ModelMap model) {
//		List<Tag> tags = tagService.topTags(maxResults, true);
        Paging page = wrapPage(1);
        page.setMaxResults(maxResults);
        page = tagService.paging(page, "", true, "featured", "hots");
        model.put("page", page);
        return getView(Views.TAGS_INDEX);
    }

    @RequestMapping("/tag_snippet/{pn}")
    public String snippet(@PathVariable Integer pn, ModelMap model, HttpServletRequest request) {
        Paging page = wrapPage(pn);
        page.setMaxResults(maxResults);

        page = tagService.paging(page, "", true, "featured", "hots");
        model.put("page", page);
        return getView(Views.TAGS_SNIPPET);
    }

    @RequestMapping("/tag/{tag}")
    public String tag(@PathVariable String tag, Integer pn, ModelMap model) {
        Paging page = wrapPage(pn);
        try {
            if (StringUtils.isNotEmpty(tag)) {
                postService.searchByTag(page, tag);
                tagService.identityHots(tag);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.put("page", page);
        model.put("tag", tag);
        return getView(Views.TAGS_TAG);
    }

}
